package Ooday04;

public class PolymorphicDemo {
    public static void main(String[] args) {

        //演示向上造型:
       /* Dog p1 = new Dog("小黑", 2, "黑");
        //p1.name/age/color/drink()/eat()/swim()/lookHome();
        Animal p2 = new Dog("小黑", 2, "黑");  //狗是动物 ------------造型
        //p2.name/age/color/drink()/eat()
        Swim p3 = new Dog("小黑", 2, "黑");  //狗会游泳 -------------造型
        //p3.swim
        Master master=new Master();
        Dog dog=new Dog("小利",21,"黄色");
        Chick chick=new Chick("小黑",2,"黑");
        Fish fish=new Fish("小白",1,"白色");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);*/
       //演示向上造型（多态）的第2点应用
        Animal[]animals=new Animal[5];
        animals[0]=new Dog("小黑",1,"黑");
        animals[1]=new Dog("小白",2,"白");
        animals[2]=new Fish("小花",1,"花");
        animals[3]=new Chick("小金",1,"金");
        animals[4]=new Fish("小灰",2,"灰");
        for (int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].drink();
            animals[i].eat();

            if (animals[i] instanceof Dog){
                Dog dog=(Dog)animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim){
                Swim s=(Swim)animals[i];
                s.swim();
            }
        }
        //演示强转成功的条件
        /*Animal o=new Dog("小黑",1,"黑");
        Dog g=(Dog)o;//引用o所指向的对象，就是Dog类型
        Swim s=(Swim)o;//引用o所指向的对象，实现了Swim接口
        //Fish f=(Fish)o;//运行时会发生ClassCastException类型转换异常

        System.out.println(o instanceof Dog);//ture
        System.out.println(o instanceof Swim);//ture
        System.out.println(o instanceof Fish);//false
        if (o instanceof Fish){//false
            Fish f=(Fish)o;
        }*/


        Animal o=new Dog();
        Dog g=(Dog)o;
        Swim s=(Swim)o;
    }
}
